<template>
  <div>
    <table cellspacing="0" class="table">
      <thead>
        <tr>
          <th width="50">
            <Checkbox v-model="selectAll" @on-change="onSelectAll"></Checkbox>
          </th>
          <th width="20"></th>
          <th v-for="item in listData" :key="item.id" :width="item.width">{{item.title}}</th>
        </tr>
      </thead>
      <tbody v-for="(item, index) in list" :key="index">
        <tr>
          <td>
            <Checkbox v-model="item.checked" @on-change="onJoinSaleCheck(item)"></Checkbox>
          </td>
          <td class="cursor-title">
            <Icon
              type="ios-arrow-down"
              size="20"
              v-if="item.showChildren"
              @click="onShowChild(item)"
            ></Icon>
            <Icon
              type="ios-arrow-forward"
              size="20"
              v-if="item.productSkuList && item.productSkuList.length && !item.showChildren"
              @click="onShowChild(item)"
            ></Icon>
          </td>
          <td :title="item.code">{{item.code}}</td>
          <td>-</td>
          <td class="cursor-title" :title="item.title">
            <div class="flex-start">
              <img :src="$store.state.moreSale.imgSrc+item.productPrimaryImg+'_90w'" alt />
              <span class="e2">{{item.title}}</span>
            </div>
          </td>
          <!-- <td v-if="isLarge">-</td>
          <td v-if="isLarge">-</td>
          <td v-if="isLarge">-</td> -->
          <td>
            <div class="e1">{{getMinMaxValue(item.productSkuList, {index: 'salePrice'})}}</div>
          </td>
          <td>{{item.isShelf | status}}</td>
          <td>{{getSum(item.productSkuList, {index: 'stock'})}}</td>
          <td>
            <Button size="small" @click="onDeleteSale(index,list)">删除</Button>
          </td>
        </tr>
        <template v-if="item.productSkuList && item.productSkuList.length">
            <tr
              v-for="(child, index1) in item.productSkuList"
              :key="index1"
              v-show="item.showChildren">
              <td></td>
              <td></td>
              <td>
                <Checkbox v-model="child.checked" @on-change="onJoinSaleCheck(child,item)"></Checkbox>
              </td>
              <td :title="child.code">{{child.code}}</td>
              <td :title="item.title" class="cursor-title">
                <div class="flex-start">
                  <img :src="$store.state.moreSale.imgSrc+item.productPrimaryImg+'_90w'" alt />
                  <span class="e2">{{item.title}}</span>
                </div>
              </td>
              <!-- <td v-if="isLarge">-</td>
              <td v-if="isLarge">-</td>
              <td v-if="isLarge">-</td> -->
              <td>
                <div class="e1">{{child.salePrice}}</div>
              </td>
              <td>{{item.isShelf | status}}</td>
              <td>{{child.stock}}</td>
              <td>
                <Button @click="onDeleteSale(index1, item.productSkuList,index,list)" size="small">删除</Button>
              </td>
            </tr>
        </template>
      </tbody>
    </table>
    <!-- <div class="pager">
      <Page
        :total="totalItem"
        :current="currentPage"
        :page-size="pageSize"
        @on-change="onPageChange"
        @on-page-size-change="onPageSizeChange"
        show-sizer
        placement="top"
      ></Page>
    </div> -->
    <Loader :status="status" @refresh="getData"></Loader>
  </div>
</template>
<script>
import {
  getMinValue,
  getMaxValue,
  getSum
} from "@/views/sales/common/TableSaleMethods";
export default {
  props: {
    list: "",
    status: "",
    listData: "",
    isLarge: {
      value: Boolean,
      default: ()=> false
    }
  },
  data() {
    return {
      selectAll: false,
      totalItem: 1,
      pageSize: window.$pageSize,
      currentPage: 1,
      viewData: []
    };
  },
  created() {
    // this.initData();
  },
  methods: {
    initData() {
      this.totalItem = this.list.length;
      this.viewData = this.list.slice(0, this.pageSize);
    },
    getSum(param, object) {
      return getSum(param, object);
    },
    getMinMaxValue(param, object) {
      if (getMinValue(param, object) == getMaxValue(param, object)) {
        return `[${getMinValue(param, object)}]`;
      } else {
        return `[${getMinValue(param, object)}-${getMaxValue(param, object)}]`;
      }
    },
    onSelectAll() {
      // 全选 、全不选
      this.$emit("onChangeSelectAll", this.selectAll);
    },
    onJoinSale(child, item) {
      this.$emit("onJoinSale", child, item);
    },
    onShowChild(item) {
      item.showChildren = !item.showChildren;
    },
    onJoinSaleCheck(child, item) {
      this.$emit("onJoinSaleCheck", child, item);
    },
    onDeleteSale(index, data, index1, data1) {
      this.$emit("onDeleteSale", index, data, index1, data1);
    },
    getData() {},
    onPageChange(val) {
      this.currentPage = val
      this.$emit('onPageChange', {size: this.pageSize, current: this.currentPage})
    },
    onPageSizeChange(val) {
      this.pageSize= val
      this.$emit('onPageSizeChange', {size: this.pageSize, current: this.currentPage})
    }
  },
  filters: {
    status(val) {
      if (val) {
        return "上架";
      } else {
        return "下架";
      }
    }
  },
  watch: {
    // list() {
    //   this.initData();
    // }
  }
};
</script>

